package com.kun.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kun.entity.Blog;
import com.kun.entity.Comment;
import com.kun.mapper.BlogMapper;
import com.kun.mapper.CommentMapper;
import com.kun.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SunKun
 * @since 2021-06-14
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private BlogMapper blogMapper;

    /**
     * 查询博客对应的评论
     * @param id
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public List<Comment> queryCommentByBlogId(int id, int state) throws Exception{
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("blogId",id);//博客id
        wrapper.eq("state",state);//审核状态
        return commentMapper.selectList(wrapper);
    }

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @Override
    public int insertComment(Comment comment) throws Exception{
        return commentMapper.insert(comment);
    }

    /**
     * 评论分页
     * @param page
     * @return
     * 前台评论显示
     */
    @Override
    public IPage<Comment> commentPage(IPage<Comment> page) {
        return commentMapper.commentPage(page);
    }

    /**
     * 更改评论
     * @param comment
     * @return
     */
    @Override
    public int updateComment(Comment comment) {
        return commentMapper.updateById(comment);
    }

    /**
     * 根据id删除评论
     * @param id
     * @return
     */
    @Override
    public int delComment(int id) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("blogId",id);
        return commentMapper.delete(wrapper);
    }
}
